#include <intrin.h>

#include "Utility.h"



namespace MemMgr
{

	// WARNING!! if iBitMap == 0 the result is undefined
	T_U32 FindLeastSignificantBitSetIndex(const T_U32 iBitMap)
	{
		#ifdef _MSC_VER
			unsigned long index;
			_BitScanForward(&index, iBitMap);
			//return 0x1f - __lzcnt(iBitMap); // Not supported on all platform
			return index;
		#else
			#error "Not defined for the current platform"
		#endif
	}



	// WARNING!! if iBitMap == 0 the result is undefined
	T_U32 FindMostSignificantBitSetIndex(const T_U32 iBitMap)
	{
		#ifdef _MSC_VER
			unsigned long index;
			_BitScanReverse(&index, iBitMap);
			return index;
		#else
			#error "Not defined for the current platform"
		#endif
	}



	T_U32 FindLeastSignificantBitSetMask(const T_U32 iBitMap)
	{
		#ifdef _MSC_VER
			#pragma warning(disable : 4146)
				return (iBitMap & -iBitMap);
			#pragma warning(default : 4146)
		#else
			#error "Not defined for the current platform"
		#endif
	}
}